package com.panasonic.web.controller.exam;

import com.panasonic.common.utils.ExcelUtil;
import com.panasonic.common.utils.bean.ListBeanUtil;
import com.panasonic.web.domain.cms.CmsExam;
import com.panasonic.web.domain.cms.CmsExamRecord;
import com.panasonic.web.mapper.CmsExamMapper;
import com.panasonic.web.service.ICmsExamRecordService;
import com.panasonic.web.vo.*;
import com.panasonic.common.annotation.Log;
import com.panasonic.common.core.controller.BaseController;
import com.panasonic.common.core.domain.AjaxResult;
import com.panasonic.common.core.page.TableDataInfo;
import com.panasonic.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 考试记录Controller
 *
 * @author lwf
 * @date 2021-11-16
 */
@RestController
@RequestMapping("/cms/record")
@Api(value = "CmsExamRecord", tags = "考试记录")
public class CmsExamRecordController extends BaseController {
    @Autowired
    private ICmsExamRecordService cmsExamRecordService;

    @Autowired
    private CmsExamMapper cmsExamMapper;
    /**
     * 查询考试记录列表
     */
    @ApiOperation(value = "查询")
    @GetMapping("/list")
    public TableDataInfo list(CmsExamRecord cmsExamRecord) {
        startPage();
        List<CmsExamRecord> list = cmsExamRecordService.selectCmsExamRecordList(cmsExamRecord);
        return getDataTable(list);
    }


    /**
     * 获取考试记录详细信息
     */
    @ApiOperation(value = "获取考试记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cmsExamRecordService.selectCmsExamRecordById(id));
    }

    /**
     * 新增考试记录
     */
    @ApiOperation(value = "新增考试记录")
    @Log(title = "考试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsExamRecord cmsExamRecord) {
        return toAjax(cmsExamRecordService.insertCmsExamRecord(cmsExamRecord));
    }

    /**
     * 修改考试记录
     */
    @ApiOperation(value = "修改考试记录")
    @Log(title = "考试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsExamRecord cmsExamRecord) {
        return toAjax(cmsExamRecordService.updateCmsExamRecord(cmsExamRecord));
    }

    /**
     * 删除考试记录
     */
    @ApiOperation(value = "删除考试记录")
    @Log(title = "考试记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsExamRecordService.deleteCmsExamRecordByIds(ids));
    }

    @ApiOperation(value = "开始考试")
    @GetMapping("/startExam/{examId}")
    public AjaxResult startExam(@PathVariable long examId) {
        CmsExamStartVo data = cmsExamRecordService.startExam(examId);
        return AjaxResult.success(data);
    }

    @ApiOperation(value = "提交试卷")
    @Log(title = "提交试卷", businessType = BusinessType.INSERT)
    @PostMapping("/batchInsert")
    public AjaxResult batchInsert(@RequestBody CmsExamRecordBatchVo cmsExamRecordBatchVo) {
        cmsExamRecordService.batchInsert(cmsExamRecordBatchVo);
        return AjaxResult.success("提交成功");
    }

    /**
     * 查询考试记录列表
     */
    @ApiOperation(value = "获取批阅列表")
    @GetMapping("/review/{examId}")
    public TableDataInfo list(@PathVariable long examId,CmsExamReviewParameter parameter) {

        parameter.setExamId(examId);
        List<CmsExamReviewVo> list = cmsExamRecordService.selectReviewList(parameter);
        return getDataTable(list);
    }
    @ApiOperation(value = "导出批阅列表")
    @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
    @GetMapping("/review/export/{examId}")
    public AjaxResult export(@PathVariable long examId,CmsExamReviewParameter parameter)
    {
        CmsExam exam = cmsExamMapper.selectCmsExamById(examId);
        if(exam==null){
            AjaxResult.error("找不到试卷信息");
        }
        parameter.setExamId(examId);
        List<CmsExamReviewVo> list = cmsExamRecordService.selectReviewList(parameter);
        List<CmsExamReviewExportVo> excelList = ListBeanUtil.copyListProperties(list, CmsExamReviewExportVo::new);
        ExcelUtil<CmsExamReviewExportVo> util = new ExcelUtil<CmsExamReviewExportVo>(CmsExamReviewExportVo.class);
        return util.exportExcel(excelList, exam.getName());
    }

    @ApiOperation(value = "获取考生试卷")
    @GetMapping("/review")
    public  TableDataInfo getExamRecordList(CmsExamReviewRequestVo requestVo ){
        List<CmsExamRecordAnswerVo> list = cmsExamRecordService.selectReviewListByUser(requestVo);
        return getDataTable(list);
    }


    @ApiOperation(value = "批改试卷")
    @Log(title = "批改试卷", businessType = BusinessType.UPDATE)
    @PostMapping("/batchUpdate")
    public AjaxResult batchUpdate(@RequestBody CmsExamRecordBatchVo cmsExamRecordBatchVo) {
        cmsExamRecordService.batchUpdate(cmsExamRecordBatchVo);
        return AjaxResult.success("提交成功");
    }

    @ApiOperation(value = "重置考试")
    @GetMapping("/reset/{examId}")
    public AjaxResult doRestExam(@PathVariable long examId) {
        cmsExamRecordService.doRestExam(examId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "批改完成")
    @GetMapping("/correctionCompleted/{examId}")
    public AjaxResult correctionCompleted(@PathVariable long examId) {
        cmsExamRecordService.correctionCompleted(examId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "复制考试")
    @GetMapping("/replicate/{examId}")
    public AjaxResult replicate(@PathVariable long examId) {
        cmsExamRecordService.replicate(examId);
        return AjaxResult.success();
    }
}
